package com.kinon.ogt.view.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.kinon.ogt.R;
import com.kinon.ogt.api.OgtApi;
import com.kinon.ogt.api.service.ServiceFactory;
import com.kinon.ogt.utils.RxUtils;
import com.kinon.ogt.utils.SPrefsUtils;
import com.kinon.ogt.view.widget.X5WebView;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.Disposable;

/**
 * 佛祖保佑 永无BUG
 *
 * @author WangZhiYao
 * @date 2018/4/25
 */
public class MyFarmFragment extends Fragment {

    @BindView(R.id.webView)
    X5WebView mWebView;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;
    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;

    private Unbinder mUnBinder;
    private String mUserToken;

    private boolean isClearCache = false;
    private String mCurrentVersion = "";

    private OgtApi mOgtApi;
    private Disposable mDisposable;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my_farm, container, false);
        mUnBinder = ButterKnife.bind(this, rootView);

        initData();
        //initView();

        return rootView;
    }

    private void initData() {
        mUserToken = SPrefsUtils.readToken();
        if (TextUtils.isEmpty(SPrefsUtils.readVersion())) {
            SPrefsUtils.writeVersion("1.0.0");
        }

        mOgtApi = ServiceFactory.getOgtApi();

        mDisposable = mOgtApi.getCurrentVersion()
                .compose(RxUtils.rxSchedulerHelper())
                .compose(RxUtils.handleResult())
                .subscribe(s -> {
                    mCurrentVersion = s;
                    if (!mCurrentVersion.equals(SPrefsUtils.readVersion())) {
                        isClearCache = true;
                        SPrefsUtils.writeVersion(mCurrentVersion);
                    }
                    initView();
                });

    }

    private void initView() {
        //String gameUrl = "http://test.jishi88.cn/ogtf/game.html?token=";

        String gameUrl = "https://ogtchain.com/game/" + mCurrentVersion + "/index.html?token=";

        mSwipeRefresh.setEnabled(false);

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int i) {
                if (i == 100) {
                    mProgressBar.setVisibility(View.GONE);
                    return;
                }

                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.setProgress(i);

                super.onProgressChanged(webView, i);
            }
        });

        mWebView.clearCache(isClearCache);
        mWebView.loadUrl(gameUrl + mUserToken);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
        mDisposable.dispose();
    }
}
